package com.caishi.lkx.exam.api;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.caishi.lkx.common.AdminBaseApi;
import com.caishi.lkx.exam.model.ColumnModel;
import com.caishi.lkx.exam.model.ProductBaseModel;
import com.caishi.lkx.exam.service.*;
import com.zzw.common.Wrappers;

import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;


@Tag(name = "科目产品管理")
@Slf4j
@RestController
@RequestMapping("/exam/product/base")
public class ProductBaseApi extends AdminBaseApi<IProductBaseService, ProductBaseModel, String> {
    @Resource
    private IProductBaseService productBaseService;

    @Resource
    private IExamService examService;

    @Resource
    private IIndustryService industryService;

    @Resource
    private ICategoryService categoryService;

    @Resource
    private IColumnService columnService;

    @Override
    public IProductBaseService loadService() {
        return productBaseService;
    }

    @Override
    protected LambdaQueryWrapper<ProductBaseModel> baseWrapper(ProductBaseModel model, JSONObject otherParam) {
        return super.baseWrapper(
                        model.clearEmptyStringField("name"), otherParam)
                .like(StrUtil.isNotBlank(model.getName()), ProductBaseModel::getName, model.getName());
    }

    @Override
    protected List<ProductBaseModel> listCover(List<ProductBaseModel> result, JSONObject otherParam) {
        if (otherParam.getBool("base", false)) {
            result = result.parallelStream().peek(r -> {
                if (StrUtil.isNotBlank(r.getIndustryId())) {
                    r.setIndustryName(industryService.queryName(r.getIndustryId()));
                }
                if (StrUtil.isNotBlank(r.getExamId())) {
                    r.setExamName(examService.queryName(r.getExamId()));
                }
                if (null != r.getCategoryId()) {
                    r.setCategoryName(categoryService.queryName(r.getCategoryId()));
                }
                r.setColumnName(columnService.queryNames(r.getColumnIds()));
            }).collect(Collectors.toList());
        }

        return result;
    }

    @GetMapping("/list")
    public List<ProductBaseModel> list(@RequestParam(defaultValue = "10") Integer size, @ModelAttribute ProductBaseModel model, @NotNull JSONObject otherParam) {
        if (size > 3000) {
            size = 3000;
        }
        return listCover(loadService().selectList(loadService().wrapperProcessing(baseWrapper(model, otherParam).last(" limit " + size))), otherParam);
    }

    @GetMapping("/column-details")
    public List<ColumnModel> columnDetails(@RequestParam String id) {
        ProductBaseModel product = productBaseService.getById(id);
        if (null == product || CollUtil.isEmpty(product.getColumnIds())) {
            return Collections.emptyList();
        }
        return columnService.selectBatchIds(product.getColumnIds()).parallelStream().peek(r -> {
            if (null != r.getCategoryId()) {
                r.setCategoryName(categoryService.queryName(r.getCategoryId()));
            }
        }).collect(Collectors.toList());
    }
    @GetMapping("/column-details-by-productIds")
    public List<ColumnModel> columnDetailsByIds(@RequestParam String[] ids) {
        List<ProductBaseModel> productBaseModels = productBaseService.selectList(Wrappers.<ProductBaseModel>lambdaQuery().in(ProductBaseModel::getId, ids));
        if (null == productBaseModels ) {
            return Collections.emptyList();
        }
        List<String> columnIds=new ArrayList< String>();
        productBaseModels.forEach(e->{
            columnIds.addAll(e.getColumnIds());
        });
//        QueryWrapper<ColumnModel> wrapper1 = new QueryWrapper<>();
//        wrapper1.in("id", columnIds);
//        columnService.selectList(wrapper1);
        return columnService.selectBatchIds(columnIds);
    }

    @GetMapping("/listByCategoryIds")
    public List<ProductBaseModel> columnDetails(@RequestParam List<String> categoryIds) {
        if (CollUtil.isNotEmpty(categoryIds)) {
            return productBaseService.selectList(Wrappers.<ProductBaseModel>lambdaQuery().in(ProductBaseModel::getCategoryId, categoryIds));
        }
        return Collections.emptyList();
    }
    /**
     * 获取产品列表,对应的栏目，标识栏目类型等
     * */
    @GetMapping("/listByProductIds")
    public List<ProductBaseModel> listByProductIds(@RequestParam List<String> productIds) {
        QueryWrapper<ProductBaseModel> wrapper = new QueryWrapper<>();
        wrapper.in("id",productIds);
        List<ProductBaseModel> productBaseModels = productBaseService.selectList(wrapper);
        return productBaseModels;
    }
}
